//
//  ViewController.m
//  UITableViewDemo
//
//  Created by guopanpan on 13-6-21.
//  Copyright (c) 2013年 guopanpan. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)dealloc
{
    [_flagArray release];
    [_demoTableView release];
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    self.flagArray = [NSMutableArray arrayWithCapacity:10];
    [self.flagArray addObject:@"YES"];
    [self.flagArray addObject:@"YES"];
    [self.flagArray addObject:@"YES"];
    [self.flagArray addObject:@"YES"];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -
#pragma mark UITableView DataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSString *flag = [self.flagArray objectAtIndex:section];
    if ([flag isEqualToString:@"YES"]) {
        return 10;
    } else {
        return 0;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"CustomCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:cellIdentifier] autorelease];
    }
    cell.textLabel.text = @"test";
    
    return cell;
}

#pragma mark -
#pragma mark - UITableView Delegate
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 22)];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(0, 0, 320, 22);
    button.tag = section;
    [button setTitle:[NSString stringWithFormat:@"press me %i", section] forState:UIControlStateNormal];
    [button addTarget:self
               action:@selector(tapOnButton:)
     forControlEvents:UIControlEventTouchUpInside];
    
    [headerView addSubview:button];
    
    return [headerView autorelease];
}

#pragma mark -
#pragma mark UIControl Target
- (void)tapOnButton:(id)sender
{
    UIButton *button = (UIButton *)sender;
    if ([[self.flagArray objectAtIndex:button.tag] isEqualToString:@"YES"]) {
        [self.flagArray replaceObjectAtIndex:button.tag withObject:@"NO"];
    } else {
        [self.flagArray replaceObjectAtIndex:button.tag withObject:@"YES"];
    }
    
    [self.demoTableView reloadData];
}

@end
